[GENERAL] Tricky SQL query (tried [SQL])

Поиск
Список
Период
Сортировка
От stuart@ludwig.ucl.ac.uk (Stuart Rison)
Тема [GENERAL] Tricky SQL query (tried [SQL])
Дата
Msg-id v0153051ab2c220251c26@[128.40.242.176]
обсуждение исходный текст
Ответы Re: [GENERAL] Tricky SQL query (tried [SQL])  (Dustin Sallings <dustin@spy.net>)
Список pgsql-general
Dear All,

I'm having a lot of trouble figuring out a good SQL query for the situation
below.

Consider the following table:

dev_brecard=> select * from test order by person;
person|fruit
- ------+---------
lucy  |mandarins
lucy  |tomatoes
lucy  |pears
lucy  |oranges
lucy  |apples
peter |pears
peter |apples
peter |oranges
peter |prunes
robert|figs
robert|dates
stuart|apples
stuart|pears
stuart|prunes
stuart|bananas
stuart|kumquats
(16 rows)

(code for creating and populating table is in a PS at the end of this posting)

You can assume that the table is appropriately normalised and that there is
a composite primary key for it (i.e. each COMBINATION of person and fruit
will appear only once and neither of the fields can be NULL)

How do I select from all person who like 'pears' and 'apples' (in this
case, lucy, peter and stuart) or 'pears', 'apples' and 'oranges' (in this
case, lucy and peter)?

I re-read my SQL books but I am still somewhat stumped.  Things I could
think of for that sort of query:

1) Select all persons who like 'pears'; Select all persons who like
'apples'; Select all persons who like 'oranges'; Calculate the INTERSECTION
of these sets.  But does postgreSQL have a INTERSECTION operator?

2) Use nested subselects:

Select person from test where fruit='pears' and person in (
        Select person from test where fruit='apples' and person in (
                Select person from test where fruit='oranges'
        )
)

But how efficient will this be if I start looking for 6 or seven fruits in
a table with hundreds of entries?

3) Am I storing this sort of data in to wrong kind of form (should I
somehow denormalise?  if so, how?)?

Any suggestions????

thanks for any help out there!

Stuart.

PS.  Code to cut and paste for table:

create table test (person    varchar(25), fruit     varchar(25));
insert into test values ('stuart','apples');
insert into test values ('stuart','pears');
insert into test values ('stuart','bananas');
insert into test values ('stuart','kumquats');
insert into test values ('peter','oranges');
insert into test values ('peter','prunes');
insert into test values ('lucy','mandarins');
insert into test values ('lucy','tomatoes');
insert into test values ('peter','apples');
insert into test values ('lucy','apples');
insert into test values ('peter','pears');
insert into test values ('lucy','pears');
insert into test values ('lucy','oranges');
insert into test values ('stuart','prunes');
insert into test values ('robert','figs');
insert into test values ('robert','dates');

+-------------------------+--------------------------------------+
| Stuart Rison            | Ludwig Institute for Cancer Research |
+-------------------------+ 91 Riding House Street               |
| Tel. (0171) 878 4041    | London, W1P 8BT, UNITED KINGDOM.     |
| Fax. (0171) 878 4040    | stuart@ludwig.ucl.ac.uk              |
+-------------------------+--------------------------------------+



В списке pgsql-general по дате отправления:

Предыдущее
От: Silvio Emanuel Barbosa de Macedo
Дата:
Сообщение: Re: linux/unix ODBC driver
Следующее
От: Silvio Emanuel Barbosa de Macedo
Дата:
Сообщение: Re: Web-Based Reports (cgi using mod_perl)